package CutLoad.transitions.cloud
{
	/**
	 * 弹性弹出窗口效果
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	import CutLoad.CutLoad;
	import CutLoad.cookies.Painter;
	import CutLoad.cookies.RegPointOperation;
	import CutLoad.transitions.Record;
	import CutLoad.transitions.SpringMove;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class SpringPopup
	{
		private var imgCloud:Bitmap;
		private var atParent:DisplayObject
		private var target:Sprite;
		private var targetX:Number,targetY:Number;
		
		private var regPoint:RegPointOperation;
		
		private var eff_spring:SpringMove;
		
		private var record:Record=null;
		//是否需要倒放
		private var isReverse:Boolean;
		/**
		 * 初始化 
		 * @param target 要模仿的目标
		 * param  isReverse  是否可以重放
		 * @param targetIsHide 模仿的目标是否隐藏 默认：true隐藏   false 不 隐藏
		 * 
		 */		
		public function SpringPopup(target:Sprite,isReverse:Boolean=true,targetIsHide:Boolean=true)
		{
			this.target=target;
			this.isReverse=isReverse;
			target.visible=!targetIsHide;
			//把要显示的对象变成图像
			imgCloud=Painter.getImg(target);
			imgCloud.x=target.x;
			imgCloud.y=target.y;
			imgCloud.addEventListener(Event.ENTER_FRAME,enterFrame);
			target.parent.addChild(imgCloud);
			regPoint=new RegPointOperation(imgCloud,new Point(imgCloud.width/2,imgCloud.height/2))
			//
			eff_spring=new SpringMove(0,10,0.2,0.75);
			eff_spring.setFunEnterFrame(function(num:Number):void{
				var temXY:Number=num*0.1;
				regPoint.setScaleXY(temXY,temXY);
			});
			eff_spring.setFunEnd(function():void{
				imgCloud.removeEventListener(Event.ENTER_FRAME,enterFrame);
				//如果有回放的话，要时刻注意目标的移动位置
				imgCloud.addEventListener(Event.ENTER_FRAME,enterFrame_saveXY);
				if(imgCloud.parent!=null) imgCloud.parent.removeChild(imgCloud);
				target.visible=true;
			});
			targetX=this.target.x;
			targetY=this.target.y;
			eff_spring.start();
			
		}
		/**
		 * 倒放 
		 * 
		 */		
		public function ReverseStart():void{
			if(!isReverse) throw new Error('此方法调用无效，请确保初始化方法时设置了可以倒序');
			//if(target.parent==null) throw new Error("目标没有显示出来，它的parent==null");
			imgCloud=Painter.getImg(target);
			imgCloud.x=targetX
			imgCloud.y=targetY
			target.parent.addChild(imgCloud);
			//
			record=eff_spring.getRecord();
			record.setIsAd(false);
			regPoint=new RegPointOperation(imgCloud,new Point(imgCloud.width/2,imgCloud.height/2))
			imgCloud.removeEventListener(Event.ENTER_FRAME,enterFrame_saveXY);
			imgCloud.addEventListener(Event.ENTER_FRAME,enterFrame_reverse);
			record.setFunEnterFrame(function(num:Number):void{
				var temXY:Number=num*0.1;
				regPoint.setScaleXY(temXY,temXY);
			});
			record.setFunEnd(function():void{
				if(imgCloud.hasEventListener(Event.ENTER_FRAME)) imgCloud.removeEventListener(Event.ENTER_FRAME,enterFrame_reverse);
				if(imgCloud.parent!=null) imgCloud.parent.removeChild(imgCloud);
			});
			record.start();
		}
		/**
		 * 获得参考的对象 
		 * @return 
		 * 
		 */		
		public function getTarget():Sprite{
			return target
		}
		private function enterFrame(e:Event):void{
			eff_spring.enterFrame();
		}
		private function enterFrame_saveXY(e:Event):void{
			if(this.target!=null){
				targetX=this.target.x;
				targetY=this.target.y;
			}
		}
		private function enterFrame_reverse(e:Event):void{
			if(record!=null) record.enterFrame();
		}
		
	}
}